////
//
/// \file   ./src/compound_name_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/compound_name_node.hpp>

#include <cassert>
#include <string>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/dereferenced_name_node.hpp>
#include <inc/parser/indexed_name_node.hpp>
#include <inc/parser/scoped_name_node.hpp>

namespace inc {
namespace parser {

using boost::scoped_ptr;

/// \brief  Default empty constructor
/// \author Chandler Carruth
/// \date   2008.05.16

CompoundNameNode::CompoundNameNode() {}

/// \brief  Empty destructor
/// \author Chandler Carruth
/// \date   2008.05.16

CompoundNameNode::~CompoundNameNode() {}

/// \brief  Parse any compound name representation
/// \author Chandler Carruth
/// \date   2007.07.26
///
/// This builds a node by attempting to parse any of a set of compound name
/// representations. Each representation is asked to complete the parse, and
/// the successful node is retained and used to answer queries to this node.

bool
CompoundNameNode::parse( const iterator &first,
                         const iterator &last,
                         iterator &last_parsed )
{
  assert( get_text().empty() && !node_ptr
          && "This node was parsed more than once!" );

  //  Attempting to parse a label node.
  node_ptr.reset( new DereferencedNameNode );
  if( node_ptr->parse( first, last, last_parsed ) )
  {
    set_text( node_ptr->get_text() );
    return true;
  }

  node_ptr.reset( new IndexedNameNode );
  if( node_ptr->parse( first, last, last_parsed ) )
  {
    set_text( node_ptr->get_text() );
    return true;
  }

  node_ptr.reset( new ScopedNameNode );
  if( node_ptr->parse( first, last, last_parsed ) )
  {
    set_text( node_ptr->get_text() );
    return true;
  }

  //  No valid parse found.
  node_ptr.reset();
  return false;
}

/// \brief  Accessor for a child dereferenced name
/// \author Chandler Carruth
/// \date   2008.05.16

DereferencedNameNode* CompoundNameNode::get_dereferenced_name() const
{
  return dynamic_cast<DereferencedNameNode*>( node_ptr.get() );
}

/// \brief  Accessor for a child scoped name
/// \author Chandler Carruth
/// \date   2008.05.16

ScopedNameNode* CompoundNameNode::get_scoped_name() const
{
  return dynamic_cast<ScopedNameNode*>( node_ptr.get() );
}

/// \brief  Accessor for a child indexed name
/// \author Chandler Carruth
/// \date   2008.05.16

IndexedNameNode* CompoundNameNode::get_indexed_name() const
{
  return dynamic_cast<IndexedNameNode*>( node_ptr.get() );
}

} //  end parser namespace
} //  end inc namespace
